package com.jrfid.smartcabinet.demo.ui.setting

import android.content.DialogInterface
import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.blankj.utilcode.util.ToastUtils
import com.jrfid.devicemanager.library.CMDData
import com.jrfid.devicemanager.library.DeviceType
import com.jrfid.devicemanager.library.DevicesManager
import com.jrfid.smartcabinet.demo.R
import com.jrfid.smartcabinet.demo.arcface.FaceServer
import com.jrfid.smartcabinet.demo.base.BaseFragment
import com.jrfid.smartcabinet.demo.databinding.SettingFragmentBinding
import com.jrfid.smartcabinet.demo.db.AppDatabase
import com.jrfid.smartcabinet.demo.ui.MainViewModel
import com.jrfid.smartcabinet.demo.utils.Config

class SettingFragment : BaseFragment<SettingFragmentBinding, SettingViewModel>(SettingViewModel::class.java) {

    companion object {
        fun newInstance() = SettingFragment()
    }

    override fun getLayoutId(): Int = R.layout.setting_fragment

    private val mainViewModel by activityViewModels<MainViewModel>()
    private val args by navArgs<SettingFragmentArgs>()
    private lateinit var dialog: AlertDialog
    private lateinit var openTheDoorDialog: AlertDialog

    override fun setData() {
        mainViewModel.showSettingMenu.postValue(false)
        getViewModel()?.showLogoutBtn?.set(args.isLogin)
        dialog = AlertDialog.Builder(requireContext())
            .setItems(R.array.array_clear_menu_items) { dialog, which ->
                when (which) {
                    0 -> {
                        AppDatabase.instance.getAccountInfoDao().deleteAll()
                    }
                    1 -> {
                        AppDatabase.instance.getFingerInfoDao().deleteAll()
                    }
                    2 -> {
                        AppDatabase.instance.getNFCInfoDao().deleteAll()
                    }
                    3 -> {
                        FaceServer.getInstance().clearAllFaces(requireContext())
                    }
                    4 -> {
                        AppDatabase.instance.getAccountInfoDao().deleteAll()
                        AppDatabase.instance.getFingerInfoDao().deleteAll()
                        AppDatabase.instance.getNFCInfoDao().deleteAll()
                        FaceServer.getInstance().clearAllFaces(requireContext())
                    }
                }
                ToastUtils.showShort(getString(R.string.text_cleared_successfully))
            }
            .create()
        getViewModel()?.showClearDialog?.observe(this, Observer {
            if (it) {
                getViewModel()?.showClearDialog?.value = false
                dialog.show()
            }
        })
        val list = Config.getCabinetList()
        val items = arrayListOf<CharSequence>()
        list.forEach {
            items.add(it.name.get()!!.toString())
        }
        openTheDoorDialog = AlertDialog.Builder(requireContext())
            .setItems(items.toTypedArray()) { dialog, which ->
                val cabinet = list.get(which)
//                MainBoardManager.instance.sendByteData(CMDData.Cabinet.openCabinetDoor(cabinet.address.get()!!.toInt().toByte()))
                DevicesManager.instance.sendData(
                    deviceType = DeviceType.MainBoard,
                    byteArray = CMDData.MainBoard.openCabinetDoor((cabinet.address.get()?.toIntOrNull() ?: 0x01).toByte())
                )
            }.create()
        getViewModel()?.showOpenTheDoorDialog?.observe(this) {
            if (it) {
                getViewModel()?.showOpenTheDoorDialog?.value = false
                openTheDoorDialog.show()
            }
        }
    }

    override fun onDestroy() {
        mainViewModel.showSettingMenu.postValue(true)
        super.onDestroy()
    }
}